﻿@{
    ViewBag.Title = "Detail";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<form class="layui-form" action="" style="margin-left: 100px; margin-right: 100px;">
    <!-- 第一行：款项名称 -->
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md4">
            <div class="layui-form-item">
                <label class="layui-form-label">款项名称:</label>
                <div class="layui-input-block">
                    <select name="ContractId" lay-verify="required" lay-search>
                        <option value="">请选择套餐</option>
                        @foreach (var package in ViewBag.Packages)
                        {
                            <option value="@package.Id">@package.PackageName</option>
                        }
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">金额:</label>
                <div class="layui-input-block">
                    <input type="number" name="EntryPrice" lay-verify="required|number" autocomplete="off" class="layui-input">
                </div>
            </div>
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select name="Type" lay-verify="required" lay-filter="selectType">
                    <option value="0">出账</option>
                    <option value="1">入账</option>
                </select>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">创建时间</label>
                <div class="layui-input-block">
                    <input type="text" name="CreateTime" id="CreateTime" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">接收人:</label>
                <div class="layui-input-block">
                    <select name="EntryPeopleId" lay-verify="required" lay-search>
                        <option value="">请选择员工</option>
                        @foreach (var staff in ViewBag.Staffs)
                        {
                            <option value="@staff.Id">@staff.Name</option>
                        }
                    </select>
                </div>
            </div>
            <!-- 提交按钮 -->
            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">创建</button>
                </div>
            </div>
        </div>
    </div>

   
</form>

<script>
    layui.use(['laydate', 'form', 'jquery', 'layer'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;

        // 初始化日期选择器
        laydate.render({
            elem: '#CreateTime',
            type: 'datetime',
            format: 'yyyy-MM-dd '
        });

        // 监听类型选择变化
        form.on('select(selectType)', function (data) {
            // 0=出账，1=入账
            if (data.value == '1') { // 入账
                $('#receiverRow').hide();
                // 移除必填验证
                $('select[name="EntryPeopleId"]').removeAttr('lay-verify');
            } else { // 出账
                $('#receiverRow').show();
                // 添加必填验证
                $('select[name="EntryPeopleId"]').attr('lay-verify', 'required');
            }
            form.render(); // 重新渲染表单
        });

        // 初始化时检查类型值
        var initialType = $('select[name="Type"]').val();
        if (initialType == '1') {
            $('#receiverRow').hide();
            $('select[name="EntryPeopleId"]').removeAttr('lay-verify');
        }

        // 监听表单提交
        // 监听表单提交
        form.on('submit(demo1)', function (data) {
            // 如果类型是入账，清空接收人ID
            if (data.field.Type == '1') {
                data.field.EntryPeopleId = null;
            }

            // 显示加载中
            var index = layer.load(1);

            // 准备提交数据
            var postData = {
                EntryPrice: parseFloat(data.field.EntryPrice),
                Type: parseInt(data.field.Type),
                CreateTime: data.field.CreateTime,
                EntryPeopleId: data.field.Type == '1' ? null : parseInt(data.field.EntryPeopleId),
                ContractId: data.field.ContractId ? parseInt(data.field.ContractId) : null,
                FounderId: 1 // 实际应用中应该获取当前用户ID
            };

            // 发送AJAX请求
            $.ajax({
                url: '/Finance/DataInsert',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(postData),
                success: function (res) {
                    layer.close(index);
                    if (res.code === 0) {
                        layer.msg('添加成功', { icon: 1 });
                        // 关闭当前弹窗
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        // 刷新父页面表格
                        parent.table.reload('test');
                    } else {
                        layer.msg(res.msg || '操作失败', { icon: 2 });
                    }
                },
                error: function (xhr, status, error) {
                    layer.close(index);
                    layer.msg('请求失败: ' + error, { icon: 2 });
                    console.error("错误详情:", xhr.responseText);
                }
            });
            return false;
        });
        //初始话表格元素
        form.render();
    });
</script>